# -*- coding:utf-8 -*-
from flask import request, jsonify, abort
from flask_cors import CORS
from com.zzh.dao_v2.userGroupDao import UserGroupDao
import time
from . import app

localTime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))


CORS(app, supports_credentials=True)

@app.route('/queryUserGroupById', methods=['POST'])
def queryUserGroupById():

    if not request.json:
        abort(400)
    id = request.json.get("id")
    try:
        total_count, userGroupJSON= UserGroupDao().queryUserGroupById(id)
        if total_count:
            return jsonify({"message": {"code": 1, "status": "queryUserGroup OK"}, "info": userGroupJSON})
        else:
            return jsonify({"info": [], "message": {"code": 0, "status": "No data"}})
    except Exception, e:
            return jsonify({"message": {"code": 0,  "info": str(e)}})

@app.route('/queryUserGroupByCondition', methods=['POST'])
def queryUserGroupByCondition():

    if not request.json:
        abort(400)
    try:
        page = request.json['page']
        pageIndex = page["pageIndex"]
        pageSize = page["pageSize"]
    except:
        return jsonify({"message": {"code": 0, "status": "请传入 pageIndex和pageSize"}})
    try:
        conditions = request.json['condition']
    except:
        return jsonify({"message": {"code": 0, "status": "未传入指定condition"}})
    try:
        multiSort = request.json['multiSort']
    except:
        return jsonify({"message": "未传入排序规则multiSort"})
    try:
        total_count, UserGroupJSON = UserGroupDao().queryUserGroupByCondition(pageIndex=pageIndex, pageSize=pageSize,conditions=conditions, multiSort=multiSort)
        if total_count:
                return jsonify({"message": {"code": 1, "status": "queryUserGroup OK"},"total":total_count,"info":UserGroupJSON})
        else:
            return jsonify({"info": [], "message": {"code": 0, "status": "No data"}})
    except Exception, e:
            return jsonify({"message": {"code": 0,  "info": str(e)}})



@app.route('/insertUserGroup', methods=['POST'])
def insertUserGroup():

    if not request.json:
        abort(400)

    Info = {
        'groupName': request.json.get('groupName'),  # 必填，请传递userd
        'groupDes': request.json.get('groupDes'),  #
        #'loginTime': request.json.get('loginTime'),  #
        'groupLogo': request.json.get('groupLogo'),  #
        'groupStatus': request.json.get('groupStatus'),#
        'groupFee':request.json.get('groupFee')
    }
    if not Info:
        return jsonify({"message": {"code": 0, "info": "信息填写未完整"}})
    try:
        rs = UserGroupDao().addUserGroup(Info)
        if rs:
            return jsonify(
                {"massage": {"code": 1, "info": "addUserGroup OK"}})  # 添加函数调用成功，返回添加的信息（不包括userRecharge的Id）
        else:
            return jsonify({"message": {"code": 0, "info": "addUserGroup Error"}})  # 添加函数调用失败



    except Exception, e:

        return jsonify({"message": {"code": 0, "info": str(e)}})




@app.route('/updateUserGroup', methods=['POST'])
def updateUserGroup():#无需更新


    try:
        id = request.json['id']
    except:
        return jsonify({"message": {"code": 0, "status": "请传入 id"}})
    try:
        oldInfo = UserGroupDao().selectGroup(id)
    except Exception, e:
            return jsonify({"message": {"code": 0,  "info": str(e)}})

    Info = {
        'id': request.json['id'],
       'groupName': request.json.get('groupName',oldInfo[1]),  # 必填，请传递userd
        'groupDes': request.json.get('groupDes',oldInfo[2]),  #

        'groupLogo': request.json.get('groupLogo',oldInfo[3]),  #
        'groupStatus': request.json.get('groupStatus',oldInfo[4]),#
        'groupFee':request.json.get('groupFee',oldInfo[6])
    }
    if not Info:
        return jsonify({"message": {"code": 0, "info": "信息填写未完整"}})
    try:
        rs = UserGroupDao().updateUserGroup(Info)
        if rs:
            return jsonify(
                {"info": Info, "massage": {"code": 1, "info": "updateUserGroup OK"}})  # 添加函数调用成功，返回添加的信息
        else:
            return jsonify({"message": {"code": 0, "info": "updateUserGroup Error"}})  # 添加函数调用失败
    except Exception, e:
            return jsonify({"message": {"code": 0, "info": str(e)}})




@app.route('/deleteUserGroup', methods=['POST'])  # 根据id数组删除zzh_user_Group表指定记录,传入格式为 "ids":[1,2,3,4]
def deleteUserGroup():
    ids = request.json.get("ids")
    if not ids:
        return jsonify({"message":"请输入待删除的  id数组！"})
    try:
        rs = UserGroupDao().deleteGroup(ids)
        if rs:
            return jsonify({"message": {"code": 1, "info": "DeleteUserGroup OK"}})
        else:
            return jsonify({"message": {"code": 0, "info": "DeleteUserGroup Error"}})

    except Exception, e:
            return jsonify({"message": {"code": 0,  "info": str(e)}})


